var config = require('../../../utils/config')
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
const app = getApp();
const ajax = app.myRequest()
Page({
    data: {
        aaa: false,
        value1: 0,
        option1: [
            { text: '请选择类型场馆', value: 0 },
            { text: '健身馆', value: 1 },
            { text: '瑜伽馆', value: 2 },
            { text: '游泳馆', value: 3 },
            { text: '拳击馆', value: 4 },
            { text: '普拉提馆', value: 5 },
            { text: '私教工作室', value: 6 }
        ],
        checked: false,
        shop: '',
        Telephone: '',
        position: '',
        time: '',
        introduce: '',
        fileList: [],
        token: '',
        picture: [],
        telephoneText: '',
        fals: false,
        show: false,
        Photo_logo: '',
        Photo_Business: '',
        typeFales: false,
        positive: [],
        theOtherSide: [],
        errorType: false,
        errorName: false,
        errorPhone: false,
        errorTime: false,
        errorLocation: false,
        errorDetailed: false
    },

    onLoad: function (options) {
        var token = wx.getStorageSync('token')
        this.setData({ token })
    },

    onSwitch1Change(detail) {
        var errorType = this.data.errorType
        if (detail.detail == 0) {
            errorType = true
        } else if (detail.detail != 0) {
            errorType = false
        }
        this.setData({ value1: detail.detail, errorType })
    },

    // 获取电话号
    getTelephone(e) {
        var telephone = e.detail.value
        if (telephone.length == 0) {
            this.setData({ fals: true, telephoneText: '电话号不能为空' })
        }
        // else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(telephone))) {
        //     this.setData({ fals: true, telephoneText: '请输入正确电话号' })
        // } 
        else {
            this.setData({ fals: false })
        }
        this.setData({ Telephone: telephone, errorPhone: false })
    },

    // 店铺名
    getShop(e) {
        this.setData({ shop: e.detail.value, errorName: false })
    },

    // 获取店铺地址
    position() {
        var _this = this
        wx.chooseLocation({
            success: function (res) {
                _this.setData({
                    position: res.address,
                    latitude: res.latitude,
                    longitude: res.longitude,
                    errorLocation: false
                })
            }
        })
    },

    // 详细地址
    address(e) {
        this.setData({ detailedAddress: e.detail.value, errorDetailed: false })
    },

    // 介绍
    getIntroduce(e) {
        this.setData({ introduce: e.detail.value })
    },


    // 上传图片第一张
    afterRead(event) {
        const file = event.detail.file.path;
        var _this = this
        wx.uploadFile({
            url: config.domain + '/File/upload',
            filePath: file,
            name: 'file',
            header: { "Content-Type": "application/x-www-form-urlencoded" },
            formData: { 'token': _this.data.token },
            success(res) {
                var file = { ...file, url: JSON.parse(res.data) }
                var Photo_logo = file.url.data.url
                var img = config.picture + file.url.data.url
                var fileList = []
                fileList.push({ url: img })
                _this.setData({ fileList, Photo_logo })
            },
        });
    },

    // 上传身份证正面
    getPositive(event) {
        const file = event.detail.file.path;
        var _this = this
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        wx.uploadFile({
            url: config.domain + '/File/upload', // 仅为示例，非真实的接口地址
            filePath: file,
            name: 'file',
            header: { "Content-Type": "multipart/form-data" },
            formData: { 'token': _this.data.token },
            success(res) {
                var file = { ...file, url: JSON.parse(res.data) }
                var cardPositive = file.url.data.url
                var img = config.picture + file.url.data.url
                var positive = []
                positive.push({ url: img })
                _this.setData({ positive, cardPositive })
            },
        });
    },

    // 上传身份证 反面
    getTheOtherSide(event) {
        const file = event.detail.file.path;
        var _this = this
        wx.uploadFile({
            url: config.domain + '/File/upload', // 仅为示例，非真实的接口地址
            filePath: file,
            name: 'file',
            header: { "Content-Type": "multipart/form-data" },
            formData: { 'token': _this.data.token },
            success(res) {
                var file = { ...file, url: JSON.parse(res.data) }
                var cardTheOtherSide = file.url.data.url
                var img = config.picture + file.url.data.url
                var theOtherSide = []
                theOtherSide.push({ url: img })
                _this.setData({ theOtherSide, cardTheOtherSide })
            },
        });
    },

    // 删除图片营业照
    deletePictureOne() {
        this.setData({ picture: [] })
    },

    // 删除图片logo
    deletePicture() {
        this.setData({ fileList: [] })
    },

    // 删除身份证正面
    deletePositive() {
        this.setData({ positive: [] })
    },

    // 删除身份证反面
    deleteTheOtherSide() {
        this.setData({ theOtherSide: [] })
    },

    // 上传图片第二张
    getPicture(event) {
        const file = event.detail.file.path;
        var _this = this
        wx.uploadFile({
            url: config.domain + '/File/upload',
            filePath: file,
            name: 'file',
            header: { "Content-Type": "multipart/form-data" },
            formData: { 'token': _this.data.token },
            success(res) {
                var file = { ...file, url: JSON.parse(res.data) }
                var Photo_Business = file.url.data.url
                var img = config.picture + file.url.data.url
                var picture = []
                picture.push({ url: img })
                _this.setData({ picture, Photo_Business })
            },
        });
    },

    toAgreement() {
        wx.navigateTo({ url: '../agreement/index' })
    },

    time(e) {
        this.setData({ time: e.detail.value, errorTime: false })
    },

    onChange(event) {
        this.setData({ checked: event.detail })
    },

    // 提交申请
    getSubmit(e) {
        var type = this.data.value1
        var logo_image = this.data.Photo_Business
        var phone = this.data.Telephone
        var address = this.data.position + this.data.detailedAddress
        var longitude = this.data.longitude
        var latitude = this.data.latitude
        var license_image = this.data.Photo_logo
        var name = this.data.shop
        var business_hours = this.data.time
        if (this.data.checked) {
            if (type == 0) {
                Dialog.alert({ message: '请选择场馆类型' })
                this.setData({ errorType: true })
            } else if (name == '') {
                Dialog.alert({ message: '场馆名称不能为空' });
                this.setData({ errorName: true })
            } else if (name.length < 4) {
                Dialog.alert({ message: '场馆名称最少4个字' });
            } else if (phone == '') {
                Dialog.alert({ message: '手机号不能为空' })
                this.setData({ errorPhone: true })
            } else if (this.data.position == '') {
                Dialog.alert({ message: '场馆定位位置不能为空' });
                this.setData({ errorLocation: true })
            } else if (this.data.detailedAddress == '') {
                Dialog.alert({ message: '场馆详细位置不能为空' });
                this.setData({ errorDetailed: true })
            } else if (business_hours == '') {
                Dialog.alert({ message: '场馆得工作时间不能为空' });
                this.setData({ errorTime: true })
            } else if (this.data.fileList == '') {
                Dialog.alert({ message: '请上传营业执照' });
            } else if (this.data.picture == '') {
                Dialog.alert({ message: '请上传场馆LOGO' });
            }
            // else if(this.data.positive==''){
            //   Dialog.alert({
            //     message: '请上传身份证正面',
            //   });
            // } else if(this.data.theOtherSide==''){
            //   Dialog.alert({
            //     message: '请上传身份证反面',
            //   });
            // }
            else {
                wx.requestSubscribeMessage({
                    tmplIds: ['pZBlRlk_qVjPUbbS1_04XHTd_98uGbSfV5P3mLy2BWY'],
                    success(res) {
                        if (res['pZBlRlk_qVjPUbbS1_04XHTd_98uGbSfV5P3mLy2BWY'] === 'accept') {
                            // ajax.postRequest('/Gymnasium/create',{type,logo_image,phone,address,longitude,latitude,license_image,name,business_hours,card_up_image,card_down_image}).then((res)=>{
                            ajax.postRequest('/Gymnasium/create', { type, logo_image, phone, address, longitude, latitude, license_image, name, business_hours }).then((res) => {
                                if (res.data.code == 1) {
                                    wx.showModal({
                                        title: '提示',
                                        content: res.data.msg,
                                        showCancel: false,
                                        success: function (res) {
                                            wx.navigateBack()
                                        }
                                    })
                                } else {
                                    wx.showModal({
                                        title: '提示',
                                        content: res.data.msg,
                                        showCancel: false,
                                    })
                                }
                            })
                        }
                    }
                })

            }
        } else {
            Dialog.alert({ message: '请同意协议' });
        }
    },

    navigateBack() {
        wx.redirectTo({ url: '/pages/login/guide/index' })
    },
})